//
//  WmcAlbumBrowseToolView.swift
//  YPWatermarkCamera
//
//  Created by 赵新 on 2022/3/29.
//

import UIKit

public extension WmcButtonItemType {
    /// 按钮类型
    enum MediaBrowseTool {
        /// 编辑
        case edit

        /// 删除
        case delete

        /// 上传云相册
        case upload

        /// 保存
        case save

        /// 分享
        case shared

        /// 朋友圈
        case friendsCircle

        /// 微信
        case wechat

        /// 自定义
        case custom(view: UIView)
    }
}

// MARK: - [WmcMediaBrowseToolView]

open class WmcMediaBrowseToolView: WmcBaseView {
    // Open

    override open func makeUI() {
        addSubview(stackView)
        stackView.snp.makeConstraints { make in
            make.top.equalTo(6)
            make.left.equalTo(Wmc_standardEdge.left)
            make.bottom.equalTo(-6 - Wmc_safeAreaBottom)
            make.right.equalTo(-Wmc_standardEdge.right)
        }
        
        uploadBtn.addSubview(uploadRedPoint)
        uploadRedPoint.snp.makeConstraints { make in
            make.width.height.equalTo(8)
            make.top.equalTo(-1)
            make.right.equalTo(-20)
        }
    }

    // Internal

    lazy var stackView: UIStackView = {
        let stackView = UIStackView()
        stackView.axis = .horizontal
        stackView.distribution = .equalSpacing
        stackView.alignment = .fill
        stackView.spacing = 5
        return stackView
    }()

    lazy var editBtn: WmcTopImgBottomTextBtn = {
        let btn = CreateBtn(title: "编辑", image: "edit".wmc_image.yp.set(tintColor: UIColor.wmc_grayH5_606066))
        return btn
    }()

    lazy var deleteBtn: WmcTopImgBottomTextBtn = {
        let btn = CreateBtn(title: "删除", image: "delete".wmc_image.yp.set(tintColor: UIColor.wmc_grayH5_606066))
        return btn
    }()

    lazy var uploadBtn: WmcTopImgBottomTextBtn = {
        let btn = CreateBtn(title: "上传云相册", image: "edit_upload_project".wmc_image.yp.set(tintColor: UIColor.wmc_grayH5_606066))
        return btn
    }()

    lazy var saveBtn: WmcTopImgBottomTextBtn = {
        let btn = CreateBtn(title: "保存", image: "save".wmc_image.yp.set(tintColor: UIColor.wmc_grayH5_606066))
        return btn
    }()

    lazy var sharedBtn: WmcTopImgBottomTextBtn = {
        let btn = CreateBtn(title: "分享", image: "share".wmc_image)
        return btn
    }()

    lazy var friendsCircleBtn: WmcTopImgBottomTextBtn = {
        let btn = CreateBtn(title: "朋友圈", image: "friends_circle_line".wmc_image)
        return btn
    }()

    lazy var wechatBtn: WmcTopImgBottomTextBtn = {
        let btn = CreateBtn(title: "微信", image: "wechat_icon_line".wmc_image)
        return btn
    }()
    
    lazy var uploadRedPoint: UIView = {
        let view = UIView()
        view.backgroundColor = .red
        view.layer.cornerRadius = 4
        return view
    }()

    /// 按钮布局表
    var btnConstraintMap: [UIView: NSLayoutConstraint] = [:]

    /// 照片浏览代理
    var photoBrowseDelegate: WmcMediaBrowseDelegate? {
        return YPWmcManager.shared.uiDelegate.mediaBrowseDelegate
    }

    func CreateBtn(title: String, image: UIImage?) -> WmcTopImgBottomTextBtn {
        let btn = WmcTopImgBottomTextBtn(title: title, image: image!)
        btn.font = 14.wmc_font
        btn.imgTextSpacing = 6
        btn.imgSize = 32
        btn.setImageTintColor(.wmc_grayH6_75757A)
        btn.textColor = .wmc_grayH5_606066
        return btn
    }

    /// 按钮集合
    func toolViewBtns(menuList: [WmcButtonItemType.MediaBrowseTool],
                      albumType: WmcMediaBrowseVM.AlbumType,
                      mediaType: WmcMedia.MediaType) -> [UIView] {
        var defaultCaseList: [WmcButtonItemType.MediaBrowseTool] = menuList
        if let caseList = photoBrowseDelegate?.toolMenuList(.current,
                                                            albumType: albumType,
                                                            mediaType: mediaType,
                                                            defaultList: defaultCaseList)
        {
            defaultCaseList = caseList
        }
        let defaultList: [UIView] = toItemList(caseList: defaultCaseList)
        let count = defaultList.count
        let spacing = ((count - 1).yp.cgFloat * stackView.spacing)
        let width = (Wmc_screenWidth - Wmc_standardEdge.yp.horizontal - spacing) / count.yp.cgFloat
        defaultList.forEach { view in // 用于减少布局约束报错日志
            if let viewConstraint = btnConstraintMap[view] {
                view.removeConstraint(viewConstraint)
            }
            let newConstraint = view.yp.layoutWidthEqual(width)
            btnConstraintMap[view] = newConstraint
        }
        return defaultList
    }

    /// 刷新工具栏按钮
    func reloadToolMenuData(menuList: [WmcButtonItemType.MediaBrowseTool],
                            albumType: WmcMediaBrowseVM.AlbumType,
                            mediaType: WmcMedia.MediaType) {
        stackView.yp.arrangedSubviews = toolViewBtns(menuList: menuList, albumType: albumType, mediaType: mediaType)
    }

    /// 转换成按钮实体
    func toItemList(caseList: [WmcButtonItemType.MediaBrowseTool]) -> [UIView] {
        var items: [UIView] = []
        caseList.forEach { cas in
            switch cas {
            case .edit:
                items.append(editBtn)
            case .delete:
                items.append(deleteBtn)
            case .upload:
                items.append(uploadBtn)
            case .save:
                items.append(saveBtn)
            case .shared:
                items.append(sharedBtn)
            case .friendsCircle:
                items.append(friendsCircleBtn)
            case .wechat:
                items.append(wechatBtn)
            case let .custom(view: view):
                items.append(view)
            }
        }
        return items
    }
}
